package com.cruise.game.controller.admin;

import com.cruise.game.common.api.ApiResult;
import com.cruise.game.common.constants.CommonConstants;
import com.cruise.game.controller.BaseController;
import com.cruise.game.model.param.admin.AnnouncementCreateParam;
import com.cruise.game.model.vo.admin.AnnouncementVO;
import com.cruise.game.model.vo.common.PageResult;
import com.cruise.game.service.admin.AdminAnnouncementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 管理员通告控制器
 * 
 * @author cruise
 * @date 2025-02-28
 */
@Slf4j
@Api(tags = "管理员通告相关接口")
@RestController
@RequestMapping("/api/admin/announcements")
@PreAuthorize("hasRole('ROLE_ADMIN')")
public class AdminAnnouncementController extends BaseController {

    @Resource
    private AdminAnnouncementService adminAnnouncementService;

    /**
     * 获取通告列表
     */
    @ApiOperation("获取通告列表")
    @GetMapping
    public ApiResult<PageResult<AnnouncementVO>> getAnnouncementList(
            @ApiParam(value = "状态", required = false) @RequestParam(value = "status", required = false) String status,

            @ApiParam(value = "页码", required = false, defaultValue = "1") @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,

            @ApiParam(value = "每页条数", required = false, defaultValue = "10") @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        log.debug("获取通告列表 - 当前用户权限: {}", SecurityContextHolder.getContext().getAuthentication().getAuthorities());

        // 限制最大页面大小
        if (pageSize > CommonConstants.MAX_PAGE_SIZE) {
            pageSize = CommonConstants.MAX_PAGE_SIZE;
        }

        return ApiResult.success(adminAnnouncementService.getAnnouncementList(status, page, pageSize));
    }

    /**
     * 获取通告详情
     */
    @ApiOperation("获取通告详情")
    @GetMapping("/{id}")
    public ApiResult<AnnouncementVO> getAnnouncementDetail(
            @ApiParam(value = "通告ID", required = true) @PathVariable("id") Long id) {
        return ApiResult.success(adminAnnouncementService.getAnnouncementDetail(id));
    }

    /**
     * 创建通告
     */
    @ApiOperation("创建通告")
    @PostMapping
    public ApiResult<Map<String, Long>> createAnnouncement(@Validated @RequestBody AnnouncementCreateParam param) {
        Long id = adminAnnouncementService.createAnnouncement(param);
        Map<String, Long> result = new HashMap<>();
        result.put("id", id);
        return ApiResult.success("添加成功", result);
    }

    /**
     * 更新通告
     */
    @ApiOperation("更新通告")
    @PutMapping("/{id}")
    public ApiResult<Boolean> updateAnnouncement(
            @ApiParam(value = "通告ID", required = true) @PathVariable("id") Long id,

            @Validated @RequestBody AnnouncementCreateParam param) {
        return ApiResult.success("更新成功", adminAnnouncementService.updateAnnouncement(id, param));
    }

    /**
     * 删除通告
     */
    @ApiOperation("删除通告")
    @DeleteMapping("/{id}")
    public ApiResult<Boolean> deleteAnnouncement(
            @ApiParam(value = "通告ID", required = true) @PathVariable("id") Long id) {
        return ApiResult.success("删除成功", adminAnnouncementService.deleteAnnouncement(id));
    }

    /**
     * 更新通告状态
     */
    @ApiOperation("更新通告状态")
    @PutMapping("/{id}/status")
    public ApiResult<Boolean> updateAnnouncementStatus(
            @ApiParam(value = "通告ID", required = true) @PathVariable("id") Long id,

            @ApiParam(value = "状态", required = true) @RequestParam("status") String status) {
        return ApiResult.success("状态已更新", adminAnnouncementService.updateAnnouncementStatus(id, status));
    }
}